package com.six666.medicaldevicetestingsystem.controller;

import com.six666.medicaldevicetestingsystem.entity.BaseResponse;
import com.six666.medicaldevicetestingsystem.entity.Project;
import com.six666.medicaldevicetestingsystem.service.Excel_Service;
import com.six666.medicaldevicetestingsystem.service.File_Service;
import com.six666.medicaldevicetestingsystem.service.Project_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class Project_Controller {
    @Autowired
    private Project_Service project_service;
    @Autowired
    private Excel_Service excel_service;
    @Autowired
    private File_Service file_service;
    private String uploadDir;

    @Autowired
    public void FileStorageService(Environment env) {
        this.uploadDir = env.getProperty("file.upload-dir");
    }

    @GetMapping("/all_project")
    public BaseResponse<List<Project>> getAllProject() {
        List<Project> data=new ArrayList<>(project_service.get_all_project());
        return new BaseResponse<>(data);

    }

    @PostMapping("/search_project")
    public BaseResponse<List<Project>> getByKeyword(@RequestParam Map<String,String> params) {
        String keyword = params.get("keyword");
        if (keyword == null) {
            return new BaseResponse<>(101, null, "参数错误", "关键词为空");
        }
        List<Project> data = new ArrayList<>(project_service.get_by_keyword(keyword));
        return new BaseResponse<>(data);
    }

    @PostMapping("/get_allsn")
    public BaseResponse<List<String>> getAllSn() {
        List<String> sn = project_service.get_all_sn();
        return new BaseResponse<>(sn);
    }
    @PostMapping("/gen_project_by_excel")
    public BaseResponse<Integer> uploadExcelFile(@RequestParam("file") MultipartFile file) {
        try {
            String filePath = this.uploadDir +"\\excel\\" + file.getOriginalFilename();
            file_service.save_file(file,"excel",file.getOriginalFilename());
            List<List<String>> data = excel_service.readExcelFile(filePath);
            int res=project_service.gen_project(data);
            return new BaseResponse<>(res);
        } catch (IOException e) {
            e.printStackTrace();
            return new BaseResponse<>(-1);
        }
    }
    @PostMapping("/get_new_project")
    public BaseResponse<Boolean> saveProject(@RequestParam Map<String,String> params) {
        String project_name = params.get("project_name");
        String project_type = params.get("project_type");
        String categories = params.get("categories");
        String standard_number = params.get("standard_number");
        return new BaseResponse(project_service.save_new_project(project_name,project_type,categories,standard_number));
    }
}
